home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1999 Macromedia, Inc. All rights reserved.
-
- //*************** GLOBALS *****************
-
- var HELP_DOC = HELP_cmdSaveToGallery;
-
- var KO_LIST = '';
- var FULL_SRC = '';
- var DEFAULT_IMG = '';
-
- var CAT_PICK = '';
- var OBJ_PICK = '';
-
- var CAT_TYPE = '';
- var CAT_LIST = '';
- var CAT_NAME = '';
- var BROWSER_TYPE = '';
- var OBJ_TYPE = '';
- var OBJ_LIST = '';
- var OBJ_NAME = '';
- var IMG_ICON = '';
- var IMG_NAME = '';
-
-
- //******************* API **********************
-
- function commandButtons(){
- return new Array(BTN_OK,"cmdOK()",
- BTN_Cancel,"cmdCancel()",
- BTN_Help, "displayHelp()");
- }
-
-
- function cmdOK() {
- if (saveKO()) {
- garbageCollect(true);
- window.close();
- }
- }
-
- function cmdCancel() {
- garbageCollect(true);
- window.close();
- }
-
-
- function canAcceptCommand() {
- var theSel, retVal = false;
- var docDOM = dw.getDocumentDOM();
- if (docDOM) {
- retVal = (getEnclosingAttainObj() != null);
- if (!retVal) {
- theSel = docDOM.getSelection();
- retVal = isValidAttainObjSelection(theSel);
- }
- }
- return retVal;
- }
-
-
- //***************** LOCAL FUNCTIONS ******************
-
- //Returns the simple name of the template or category file name
- function getSimpleName(theName) {
- var index, retVal = theName;
- index = retVal.search(/\d\d\d_/);
- if (index == 0)
- retVal = retVal.substring(4);
- index = retVal.search(/_\d\d\.agt/);
- if (index >= 0)
- retVal = retVal.substring(0, index);
- return retVal;
- }
-
-
- function getFullCatName(theName) {
- var retVal = theName;
- if (retVal.search(/\d\d\d_/) != 0) {
- var curNum, prefixNum = 100;
- var templateDir = new File(FILE_templateUrl);
- var dirList = templateDir.list("*.*", "dirs");
- if (dirList) {
- for (var i=0; i < dirList.length; i++) {
- if (getSimpleName(dirList[i]) == theName) {
- retVal = dirList[i];
- return retVal;
- }
- curNum = parseInt(dirList[i].substring(0,3));
- if (curNum != 'NaN' && curNum >= prefixNum) prefixNum = curNum + 1;
- }
- }
- retVal = prefixNum.toString() + "_" + theName;
- }
- return retVal;
- }
-
-
- function getFullObjName(theName, theCategory) {
- var retVal = theName;
- if (retVal.search(/\d\d\d_/) != 0) {
- var curNum, prefixNum = 100;
- var category = new File(FILE_templateUrl + File.separator + theCategory);
- var fileList = category.list("*.agt");
- if (fileList) {
- for (var i=0; i < fileList.length; i++) {
- if (getSimpleName(fileList[i]) == theName) {
- retVal = fileList[i];
- return retVal;
- }
- curNum = parseInt(fileList[i].substring(0,3));
- if (curNum != 'NaN' && curNum >= prefixNum) prefixNum = curNum + 1;
- }
- }
- retVal = prefixNum.toString() + "_" + theName;
- }
- if (retVal.search(/_\d\d\.agt/) == -1) {
- if (BROWSER_TYPE[0].checked)
- retVal += "_04.agt";
- else
- retVal += "_03.agt";
- }
- return retVal;
- }
-
-
- function saveKO() {
- var retVal = false;
- var category = '';
- var object = '';
- var theDir;
- var templateFile, template;
- var imageFile, image, targetImage, target;
-
- // check that we have complete information
- if (CAT_TYPE[0].checked) {
- if (!CAT_LIST.id) {
- alert(MSG_enterCat);
- return retVal;
- }
- category = CAT_LIST.id;
- }
- if (CAT_TYPE[1].checked) {
- if (CAT_NAME.value.search(/\w/) == -1) {
- alert(MSG_enterCat);
- return retVal;
- }
- category = CAT_NAME.value;
- }
- if (OBJ_TYPE[0].checked) {
- if (!OBJ_LIST.id) {
- alert(MSG_enterObj);
- return retVal;
- }
- templateFile = OBJ_LIST.id.template;
- }
- if (OBJ_TYPE[1].checked) {
- var val0 = legalizeObjName(OBJ_NAME.value);
- if (val0.search(/\w/) == -1) {
- alert(MSG_enterObj);
- return retVal;
- } else if (beginsWithNumber(val0)) {
- alert(MSG_nameMustStartWithLetter);
- return retVal;
- }
- OBJ_NAME.value = val0;
- object = val0;
- }
- if (IMG_NAME.search(/\w/) != -1) {
- image = new File(IMG_NAME);
- } else {
- image = new File(DEFAULT_IMG);
- }
- if (!image.exists()) {
- alert(errMsg(MSG_selectImage, image.getAbsolutePath()));
- return retVal;
- }
-
- // create the dir if needed
- if (CAT_TYPE[1].checked) {
- category = getFullCatName(category);
- var theDir = new File(FILE_templateUrl + File.separator + category);
- if (theDir.exists()) {
- alert(errMsg(MSG_categoryExists, getSimpleName(category)));
- return retVal;
- } else {
- theDir.mkdir();
- }
- }
-
- // get the full object name
- if (OBJ_TYPE[1].checked) {
- object = getFullObjName(object, category);
- // check that it doesn't exist
- templateFile = FILE_templateUrl + File.separator + category + File.separator + object;
- template = new File(templateFile);
- if (template.exists()) {
- alert(errMsg(MSG_objExists, OBJ_NAME.value));
- return retVal;
- }
- } else {
- // save the source to the template file
- template = new File(templateFile);
- if (template.exists()) {
- if (!template.canWrite()) {
- alert(errMsg(MSG_objExists, template.getName()));
- return retVal;
- } else if (!confirm(errMsg(MSG_replaceExisting, getSimpleName(template.getName())))) {
- return retVal;
- } } }
- template.setContents(FULL_SRC);
-
- // copy the image file, if specified, or if it does not exist.
- targetImage = templateFile.replace(/\.agt/, ".gif");
- target = new File(targetImage);
- if (!target.exists() || IMG_NAME.search(/\w/) != -1) {
- if (target.exists()) {
- target.remove();
- }
- image.copyTo(target.getAbsolutePath());
- }
-
- // set the name of the template for the object on the page
- if (OBJ_TYPE[1].checked && KO_LIST.length > 0) {
- for (var i=0; i < KO_LIST.length; i++) {
- KO_LIST[i].setAttribute(TEMPLATE_ATTR, category + File.separator + object);
- } }
-
- retVal = true;
- return retVal;
- }
-
-
- //Set the list of categories
- //
- function setCategoryList() {
- var templateDir = new File(FILE_templateUrl);
- var dirList = templateDir.list("*.*", "dirs");
- var dirInfo = '';
- if (dirList) {
- dirInfo = new Array();
- dirInfo.ids = new Array();
- dirInfo.names = new Array();
- for (var i=0; i < dirList.length; i++) {
- if (dirList[i].search(/\d\d\d_/)==0) {
- dirInfo.names.push(getSimpleName(dirList[i]));
- dirInfo.ids.push(dirList[i]);
- } }
- } else {
- dirInfo = new Array();
- dirInfo.ids = new Array('');
- dirInfo.names = new Array(LABEL_noCategories);
- }
- CAT_LIST.setInfo(dirInfo.names, dirInfo.ids);
- if (CAT_LIST.pickValue(CAT_PICK) && CAT_PICK)
- CAT_TYPE[0].checked = true;
- else
- CAT_TYPE[1].checked = true;
- }
-
-
- //Set the list of objects with the files that are writable.
- //
- function setObjectList() {
- var fileInfo = new Array();
- if (CAT_TYPE[0].checked) {
- var category = new File(FILE_templateUrl + File.separator + CAT_LIST.id);
- var filter = (BROWSER_TYPE[0].checked) ? "*.agt" : "*_03.agt";
- var fileList = category.list(filter);
- if (fileList.length != 0) {
- fileInfo.ids = new Array();
- fileInfo.names = new Array();
- var fileObj = new File('temp');
- for (var i=0; i < fileList.length; i++) {
- fileObj.setPath(category.getAbsolutePath() + File.separator + fileList[i]);
- if (fileList[i].search(/\d\d\d_/)==0 && fileObj.canWrite()) {
- fileInfo.names.push(getSimpleName(fileList[i]));
- idInfo = new Array();
- idInfo.value = fileList[i];
- idInfo.image = removeExtension(fileObj.getAbsolutePath()) + ".gif";
- idInfo.template = fileObj.getAbsolutePath();
- fileInfo.ids.push(idInfo);
- } }
- if (fileInfo.ids.length == 0) fileInfo.ids = null;
- }
- }
- if (fileInfo.ids == null) {
- fileInfo.ids = new Array('');
- fileInfo.names = new Array(LABEL_noObjects);
- }
- OBJ_LIST.setInfo(fileInfo.names, fileInfo.ids);
- if (OBJ_LIST.pickValue(OBJ_PICK) && OBJ_PICK)
- OBJ_TYPE[0].checked = true;
- else
- OBJ_TYPE[1].checked = true;
- }
-
-
- function initializeUI() {
- var theObj, theSel, templateFile, index, browserType;
- var docDOM = dw.getDocumentDOM();
-
- if (!regCheck()) {
- window.close();
- return;
- }
-
- setNewline(); //update NEWLINE global
-
- DEFAULT_IMG = FILE_authUrl + FILE_sep + "images" + FILE_sep + "custom_attain.gif";
-
- if (getEnclosingAttainObj() != null) {
- theObj = getEnclosingAttainObj();
- theSel = docDOM.nodeToOffsets(theObj);
- } else {
- theSel = docDOM.getSelection();
- }
-
- FULL_SRC = getSelectedSrc(theSel);
-
- // get the template for the first selected object
- KO_LIST = getSelectedAttainObjs(theSel);
- templateFile = KO_LIST[0].getAttribute(TEMPLATE_ATTR);
- if (templateFile) {
- index = templateFile.indexOf('/');
- if (index != -1) {
- CAT_PICK = templateFile.substring(0, index);
- OBJ_PICK = templateFile.substring(index+1);
- } }
- // get the highest browser type
- browserType = 3;
- for (var i=0; i < KO_LIST.length; i++) {
- templateFile = KO_LIST[i].getAttribute(TEMPLATE_ATTR);
- if (templateFile && templateFile.indexOf("_04.agt") != -1)
- browserType = 4;
- }
-
- CAT_TYPE = findObject('catType');
- CAT_LIST = new PickList("catSel");
- CAT_NAME = findObject('catName');
- BROWSER_TYPE = findObject('browserType');
- OBJ_TYPE = findObject('objType');
- OBJ_LIST = new PickList("objSel");
- OBJ_NAME = findObject('objName');
- IMG_ICON = findObject('objIcon');
-
- // select the browser compatibility based on the selected object
- if (OBJ_PICK) {
- if (browserType == 4)
- BROWSER_TYPE[0].checked = true;
- else
- BROWSER_TYPE[1].checked = true;
- }
-
- setCategoryList();
- setObjectList();
-
- if (OBJ_TYPE[0].checked)
- IMG_ICON.src = OBJ_LIST.id.image;
-
- if (CAT_TYPE[1].checked) {
- CAT_NAME.focus();
- CAT_NAME.select();
- } else if (OBJ_TYPE[1].checked) {
- OBJ_NAME.focus();
- OBJ_NAME.select();
- }
- }
-
-
- function updateUI(itemName) {
- if (itemName == 'catType') {
- if (CAT_TYPE[0].checked) { // clear out the enter field
- CAT_NAME.value = '';
- }
- setObjectList();
- updateUI('objType');
- } else if (itemName == 'catSel') {
- CAT_LIST.change();
- CAT_TYPE[0].checked = true;
- updateUI('catType');
- } else if (itemName == 'catName') {
- if (isNonBlank(CAT_NAME.value)) {
- var val1 = legalizeObjName(CAT_NAME.value);
- CAT_NAME.value = val1
-
- CAT_TYPE[1].checked = true;
- updateUI('catType');
- }
- } else if (itemName == 'browserType') {
- setObjectList();
- updateUI('objType');
- } else if (itemName == 'objType') {
- if (OBJ_TYPE[0].checked && !OBJ_LIST.id) {
- OBJ_TYPE[1].checked = true;
- } else if (OBJ_TYPE[0].checked) { // clear out the enter fields
- OBJ_NAME.value = '';
- IMG_ICON.src = OBJ_LIST.id.image;
- IMG_NAME = '';
- } else {
- if (IMG_NAME)
- IMG_ICON.src = IMG_NAME;
- else
- IMG_ICON.src = DEFAULT_IMG;
- }
- } else if (itemName == 'objSel') {
- OBJ_LIST.change();
- OBJ_TYPE[0].checked = true;
- OBJ_PICK = OBJ_LIST.id.value;
- updateUI('objType');
- } else if (itemName == 'objName') {
- if (isNonBlank(OBJ_NAME.value)) {
- val1 = legalizeObjName(OBJ_NAME.value);
- OBJ_NAME.value = val1
-
- OBJ_TYPE[1].checked = true;
- }
- } else if (itemName == 'imgBrowse') {
- var result = dreamweaver.browseForFileURL("select", "Select Gallery Icon", true);
- if (result) {
- var image = new File(result);
- IMG_NAME = image.getAbsolutePath();
- IMG_ICON.src = IMG_NAME;
- }
- }
- }
-
- function legalizeObjName(theStr) {
- var retVal="";
- retVal = theStr.replace(/[\W]/g, ""); // remove non-word chars
- retVal = retVal.replace(/G(\d\d)/g, "g$1"); // remove G## matches
- if (retVal == 'length') retVal = 'lngth'; // reserved word
- return retVal
- }
-